package com.dz.controller;

import com.dz.annotation.OperationLog;
import com.dz.bean.Guide;
import com.dz.bean.Manager;
import com.dz.bean.Visitor;
import com.dz.service.LoginService;
import com.dz.util.CaptchaUtil;
import com.dz.util.Result;
import com.dz.util.SYS;
import com.dz.util.TokenUtil;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@CrossOrigin//允许跨域访问
@RequestMapping("/login")
public class LoginController {

    @Autowired
    LoginService loginService;


    @GetMapping("/captcha")
    public Result captcha(){
        return Result.success(CaptchaUtil.getCaptcha());
    }


    @PostMapping("/managerLogin")
    public Result managerLogin(@RequestBody Manager manager, @RequestParam("captcha") String captcha,@RequestParam("key") String key){
        //1.验证码验证
        if (!CaptchaUtil.checkCaptcha(key,captcha)){
            return Result.error(500, "验证码错误");
        }
        //2.登录
        try{
            String token = loginService.managerLogin(manager);
            //3.删除验证码

            return Result.success("登陆成功",token);
        }catch (Exception e){
            log.error(e.getMessage());
            return Result.error(500,e.getMessage());
        }
    }

    @PostMapping("/visitorLogin")
    public Result visitorLogin(@RequestBody Visitor visitor, @RequestParam("captcha") String captcha, @RequestParam("key") String key){
        //1.验证码验证
        if (!CaptchaUtil.checkCaptcha(key,captcha)){
            return Result.error(500, "验证码错误");
        }
        //2.登录
        try{
            String token = loginService.visitorLogin(visitor);
            //3.删除验证码

            return Result.success("登陆成功",token);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(500,e.getMessage());
        }
    }

    @PostMapping("/guideLogin")
    public Result guideLogin(@RequestBody Guide guide, @RequestParam("captcha") String captcha, @RequestParam("key") String key){
        //1.验证码验证
        if (!CaptchaUtil.checkCaptcha(key,captcha)){
            return Result.error(500, "验证码错误");
        }
        //2.登录
        try{
            String token = loginService.guideLogin(guide);
            //3.删除验证码

            return Result.success("登陆成功",token);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(500,e.getMessage());
        }
    }

    @GetMapping("/loginInfo")
    public Result loginInfo(HttpServletRequest  request) throws Exception{
        String token=request.getHeader(SYS.TOKEN_AUTHORIZATION);
        String role = TokenUtil.getRole(token);
        if (SYS.ROLE_MANAGER.equals(role)){
            Manager manager = loginService.mLoginInfo(token);
            return Result.success(manager);
        } else if (SYS.ROLE_GUIDE.equals(role)){
            Guide guide = loginService.gLoginInfo(token);
            return Result.success(guide);
        } else {
            Visitor visitor = loginService.vLoginInfo(token);
            return Result.success(visitor);
        }
    }
}
